﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PGMHelper;
using PGMHelper.ProgressForm;

using OpenSEES_Json;

namespace FiberVisible
{
    public partial class MainForm : Form
    {
        /// <summary>
        /// 截面字典
        /// </summary>
        private List<SectionInfo> Sections { set; get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 读取外部TCL文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Read_TCL_Button_Click(object sender, EventArgs e)
        {
            //初始化文件路径
            string filePath = string.Empty;
            //选择文件
            if(!WilsonHelper.OpenFile("TCL文件(*.tcl)|*.tcl|文本文档(*.txt)|*.txt|所有文件(*.*)|*.*", 
                ref filePath))
                return;

            ///****JSON测试******/
            //var jsonobj = OpenSEESInfomation.FromJson(filePath);
            ///****JSON测试******/

            //获得截面信息
            this.Sections = SectionHelper.GetSections(filePath);
            //清空ListBox
            this.SectionListbox.Items.Clear();
            //数据绑定
            this.SectionListbox.SetDataSource(this.Sections, "SectionName", "SectionName");
        }

        /// <summary>
        /// 帮助
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            HelpForm.Show(new List<string> { "Fiber Section Visible (FSV)",
                "纤维截面可视化", "https://mp.weixin.qq.com/s/T4NhnPpuMahMreqt6aEKcA",
                "20191219" }, Properties.Resources.FSV1);
        }

        #region 控件信息刷新

        /// <summary>
        /// 选择对象改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SectionListbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获得选择的对象
            var section = (SectionInfo)this.SectionListbox.GetSelectObject();
            //添加材料编号
            this.MatListbox.SetDataSource(section.FiberGroupInfoList, "MatID", "MatID");
            //全选
            this.MatListbox.CheckedAll();
            //数据绑定
            this.Mat1Combo.SetDataSource(section.FiberGroupInfoList, "MatID", "MatID");
            //刷新Textbox
            this.RefreshTextbox();
            //绘制截面
            section.ToGraphics(this.SectionPicBox, true);
        }

        /// <summary>
        /// 刷新Textbox
        /// </summary>
        /// <param name="section_name"> 截面序号 </param>
        /// <param name="isMat1"> 确定Textbox范围 </param>
        private void RefreshTextbox()
        {
            //获得纤维组
            var fiberGroup = (FiberGroup)this.Mat1Combo.SelectedItem;
            //刷新文本
            fiberGroup.ToTextBox(this.Mat1AText, this.Mat1IYText, this.Mat1IZText, this.Mat1YCText, this.Mat1ZCText);
        }

        /// <summary>
        /// Combox_Mat1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Mat_1_Combo_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.RefreshTextbox();
        }

        /// <summary>
        /// 材料选择改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MatListbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取截面类
            var section = (SectionInfo)this.SectionListbox.GetSelectObject();
            //遍历纤维组
            section.FiberGroupInfoList.ForEach(group => group.isVisible = false);
            //获得纤维组
            var fiberGroups = (this.MatListbox.GetObjects()).ConvertAll(f => (FiberGroup)f);
            //遍历纤维组可见性
            fiberGroups.ForEach(group => group.isVisible = true);
            //绘制截面
            section.ToGraphics(this.SectionPicBox, false);
        }

        #endregion

        /// <summary>
        /// 形成纤维信息
        /// 纯Fiber
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Generate_Button_Click(object sender, EventArgs e)
        {
            //获得截面
            var section = (SectionInfo)this.SectionListbox.GetSelectObject();
            //获得纤维描述
            var descpList = section.GetFiberDescp();
            //窗体实例化
            Form f = new Text(descpList);
            f.Show();
        }

        /// <summary>
        /// Picbox点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Section_DrawBox_MouseClick(object sender, MouseEventArgs e)
        {
            //记录点击坐标
            float x = e.X;
            float y = e.Y;
            //获取截面类
            var section = (SectionInfo)this.SectionListbox.GetSelectObject();
            //绘制截面并高亮显示点击纤维
            section.HighLightFiber(this.SectionPicBox, x, y, this.FiberYText, this.FiberZText, this.FiberAText);
        }

        /// <summary>
        /// 窗体大小改变触发重绘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SectionPicBox_Resize(object sender, EventArgs e)
        {
            //获得选择的对象
            var section = (SectionInfo)this.SectionListbox.GetSelectObject();
            //截面是否存在
            if (section == null) return;
            //绘制截面
            section.ToGraphics(this.SectionPicBox, true);
        }
    }
    
}
